import gc
import machine
import network
import time
import urequests
import os

pin12 = machine.Pin(12, machine.Pin.OUT)
pin13 = machine.Pin(13, machine.Pin.OUT)

url = "http://www.zengye.top/"

# 进行更新 ota升级
def OtaUpdate():
    print("检测升级")
    
    payloadLog = urequests.get(url + "/version.log")
    print("version code",payloadLog.status_code)
    print("version payload.text", payloadLog.text)
    if payloadLog.text == "1":
        print("升级开始")
        payload = urequests.get(url + "/run.py")
        code = payload.status_code
        print("code",code)
        print("payload.text", payload.text)
        with open("run.py", "w") as local_file:
            local_file.write(payload.text)
            local_file.close()
            file = open ("ota.log", "w")
            file.write("ota 1 :")
            file.close()
            print("重启 10秒后重启")
            for i in range(20):
                pin12(1)
                pin13(0)
                time.sleep(0.5);
                pin12(0)
                pin13(1)
                time.sleep(0.5);
            machine.reset()
    else:
        print("不需要升级")
